package com.zzyl.nursing.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationDto;
import com.zzyl.nursing.mapper.MemberReservationMapper;
import com.zzyl.nursing.service.MemberReservationService;
import com.zzyl.nursing.vo.ReservationPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberReservationServiceImpl extends ServiceImpl<MemberReservationMapper, Reservation> implements MemberReservationService {
    @Autowired
     private MemberReservationMapper memberReservationMapper;

    @Override
    public void add(ReservationDto dto) {
        Long userId = UserThreadLocal.getUserId();
        //拷贝
        Reservation reservation = BeanUtil.toBean(dto, Reservation.class);
        //查询是否预约
        Long count = lambdaQuery().eq(Reservation::getMobile, dto.getMobile())
                .eq(Reservation::getTime, dto.getTime()).count();
        if( count > 0){
             throw new ServiceException("该手机号已预约");
        }
        reservation.setStatus( 0);
        reservation.setCreateBy( userId.toString());
        reservation.setUpdateBy(userId.toString());
         save(reservation);
    }

    @Override
    public ReservationPage<Reservation> page(Integer pageNum, Integer pageSize, Integer status) {
        PageHelper.startPage( pageNum, pageSize);
        List<Reservation> list = memberReservationMapper.findAll(pageNum,pageSize);
        ReservationPage<Reservation> ls = new ReservationPage<Reservation>();
        if(CollectionUtil.isEmpty( list)){
        ls.setMsg("操作失败");
        ls.setCode(500);
        return ls;
        }
        Page<Reservation> page = (Page<Reservation>) list;
         ls.setTotal((int)page.getTotal());
         ls.setRows(page.getResult());
         ls.setMsg("操作成功");
          ls.setCode(200);
        return ls;
    }
}
